/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.search;

import com.asseco.mio.codex.GeoArea;
import com.asseco.mio.codex.Institution;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import com.asseco.persistence.service.HibernateUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;

/**
 *
 * @author konstantin.sivakov
 */
public class Search extends BaseFormController {

    public List searchCodex(String query, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Class t = Class.forName("com.asseco.mio.codex." + className);

        Criteria criteria = session.createCriteria(t).add(Restrictions.and(Restrictions.eq("status", true), Restrictions.ilike("name", query, MatchMode.ANYWHERE)));

        // Criteria criteria = session.createCriteria(t).add(Restrictions.like("name",query, MatchMode.ANYWHERE));
        System.out.println(criteria.toString());
        List result = criteria.list();
        return result;

    }
//    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//        Search s = new Search();

//        System.out.println(t);
//        List test = s.searchCodex(t,"Country");
//        System.out.println("ima "+test.size());
//    }
    public Institution searchInstitutionByID(String id) throws ParseException {

        List institutions = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Criteria criteria = session.createCriteria(Institution.class).add(Restrictions.eq("id", id));
        institutions = criteria.list();
        Institution i = new Institution();
        i = (Institution) institutions.get(0);
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        
        
        Date startDate = i.getStartDate();
        Date endDate = i.getEndDate();
        if (startDate!=null) {
            formatter.format(startDate);
              i.setStartDate(startDate);
        }
       if (endDate!=null) {
           formatter.format(endDate);
             i.setEndDate(endDate);
       }
      
     

        return i;
    }

    public List getGeoAreaByCountryId (String id){
List geoAreas = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

         Query q = session.createQuery("from GeoArea geoArea where geoArea.country.id like '" + id +"'");
         geoAreas = q.list();
         List<GeoArea> result = (List<GeoArea>) geoAreas;

        return result;
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        super.initBinder(request, binder);
        SimpleDateFormat dateFormat =
                new SimpleDateFormat(getText("dd/MM/yyyy", request.getLocale()));
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
    }
}
